package com.droidathletics.boundary.macrocycles;

import java.util.Calendar;
import java.util.StringTokenizer;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.droidathletics.R;
import com.droidathletics.boundary.MainMenu;
import com.droidathletics.boundary.sportsman.CreateSportsman;
import com.droidathletics.control.ManageMacrocycle;

public class CreateMacrocycle extends Activity {

	 // the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mInitialDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    initialDateYear = year;
                    initialDateMonth = monthOfYear;
                    initialDateDay = dayOfMonth;
                    updateIDateField();
                }
            };
    private DatePickerDialog.OnDateSetListener mFinalDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    finalDateYear = year;
                    finalDateMonth = monthOfYear;
                    finalDateDay = dayOfMonth;
                    updateFDateField();
                }
            };
    private void updateIDateField(){
    	initialDate.setText(new StringBuilder()
	        .append(initialDateMonth + 1).append("/")
	        .append(initialDateDay).append("/")
	        .append(initialDateYear));
    }
    private void updateFDateField(){
    	finishingDate.setText(new StringBuilder()
	        .append(finalDateMonth + 1).append("/")
	        .append(finalDateDay).append("/")
	        .append(finalDateYear));
    }
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_macrocycle);
        
        manageMacrocycle = MainMenu.manageMacrocycle;
        
        name = (EditText) findViewById(R.id.fieldCreateMacrocycleName);
        team = (EditText) findViewById(R.id.fieldCreateMacrocycleTeam);
        category = (EditText) findViewById(R.id.fieldCreateMacrocycleCategory);
        competition = (EditText) findViewById(R.id.fieldCreateMacrocycleCompetition);
        initialDate = (EditText) findViewById(R.id.fieldCreateMacrocycleInitialDate);
        finishingDate = (EditText) findViewById(R.id.fieldCreateMacrocycleFinishingDate);
        branch = (Spinner) findViewById(R.id.spinnerCMacrocycleBranch);
        
        errorData = (TextView) findViewById(R.id.textViewCreateMacrocycleError);
        
        saveButton = (Button) findViewById(R.id.createMacrocycleSave);
        
        saveButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	updateDates();
            	String errorResult = manageMacrocycle.createMacrocycle(
            			name.getText().toString(), 
            			dayStart, 
            			monthStart, 
            			yearStart, 
            			dayEnd, 
            			monthEnd, 
            			yearEnd, 
            			team.getText().toString(), 
            			branch.getSelectedItem().toString(), 
            			category.getText().toString(), 
            			competition.getText().toString());
            	
            	if ( errorResult == "")
            	{
            		finish();
                	Intent intent = new Intent(CreateMacrocycle.this, MainMenu.class);
                	//MainMenu.addDrawable();
                    startActivity(intent);
            	}
            	else
            	{
            		errorData.setText(errorResult);
            	}

            }
        });
        
    	pickIDateButton = (Button)findViewById(R.id.buttonCMacroInitial); 
        
        pickIDateButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(INITIAL_DATE_DIALOG_ID);
            }
        });
        
        pickFDateButton = (Button)findViewById(R.id.buttonCMacroFinal); 
        
        pickFDateButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(END_DATE_DIALOG_ID);
            }
        });
        
     // get the current date
        final Calendar c = Calendar.getInstance();
        initialDateYear = finalDateYear = c.get(Calendar.YEAR);
        initialDateMonth = finalDateMonth= c.get(Calendar.MONTH);
        initialDateDay = finalDateDay = c.get(Calendar.DAY_OF_MONTH);
	}
	
	@Override
    public void onBackPressed() {
		finish();
        Intent intent = new Intent(CreateMacrocycle.this, MainMenu.class);
        startActivity(intent);
	    return;
    }
	
	@Override
	protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    case INITIAL_DATE_DIALOG_ID:
	        return new DatePickerDialog(this,
	                    mInitialDateSetListener,
	                    initialDateYear, initialDateMonth, initialDateDay);
	    case END_DATE_DIALOG_ID:
	    	return new DatePickerDialog(this,
                    mFinalDateSetListener,
                    finalDateYear, finalDateMonth, finalDateDay);
	    }
	    return null;
	}
	
	private void updateDates(){
		StringTokenizer tokensStartDate = new StringTokenizer(initialDate.getText().toString(),"/");
		dayStart = tokensStartDate.nextToken();
		monthStart = tokensStartDate.nextToken();
		yearStart = tokensStartDate.nextToken();
		StringTokenizer tokensEndDate = new StringTokenizer(finishingDate.getText().toString(),"/");
		dayEnd = tokensEndDate.nextToken();
		monthEnd = tokensEndDate.nextToken();
		yearEnd = tokensEndDate.nextToken();
	}
	
	static final int INITIAL_DATE_DIALOG_ID = 0;
	static final int END_DATE_DIALOG_ID = 1;
	private Button saveButton,pickIDateButton,pickFDateButton;
	private int initialDateYear,initialDateMonth,initialDateDay;
	private int finalDateYear,finalDateMonth,finalDateDay;
	private ManageMacrocycle manageMacrocycle;
	private EditText name; 
	private EditText team;
	private EditText category;
	private EditText competition;
	private EditText initialDate;
	private EditText finishingDate;
	private Spinner branch;
	private TextView errorData;
	private String dayStart;
	private String monthStart;
	private String yearStart;
	private String dayEnd;
	private String monthEnd;
	private String yearEnd;
	
}